<template>
  <div id="UpdateUserName">
       <!--    导航栏 -->
         <van-nav-bar
            title="用户名修改"
            left-arrow
            :fixed=true
            :border=true
            @click-left="$router.go(-1)"
         ></van-nav-bar>
         <div style="margin-top:3rem;">

              <van-field
                v-model="name"
                name="用户名"
                label="新用户名"
                placeholder="请输入新的用户名"
              />
              <div style="margin: 16px;">
                <van-button round block type="primary" @click="onSubmit()">
                  更新保存
                </van-button>
              </div>
         </div>
  </div>
</template>

<script>
import { DatetimePicker, NoticeBar ,Toast} from 'vant'
import { getUserByName,UpdateUserName } from './../../../../service/api/index.js'
import {mapState,mapMutations} from 'vuex'
	export default{
     name:"Pay",
     data(){
         return{
            name:'',
              
         }
     },
      
      mounted(){
        //console.log(this.$route);
        // console.log(this.userInfo.token)
      },

      computed:{
          ...mapState(["userInfo"]),

     },
     methods:{
      ...mapMutations(['UPDATE_NAME']),

         async onSubmit() {
         //   console.log(this.userInfo.token);
          // console.log(this.name);
             let result = await getUserByName(this.name);
            // console.log(result.status);
              //判断用户名是否存在
                if (result.status==='200') {
                   Toast.fail('该用户名已存在，请重新输入!'); 
                   return ;
               };
               //判断输入用户名是否为空
              if (this.name==='') {
                  Toast.fail('用户名不能为空');
                   return ;
              };
              //修改用户名
               let res = await UpdateUserName(this.userInfo.token,this.name);
               console.log(res);
               if (res.status==='200') {
                   //提示更新更改
                  Toast.success('更新成功');
                  //保存本地  
                  let name = this.name;
                 //  console.log(name);
                   this.UPDATE_NAME({name});
               }
        },

          
     }

	}
</script>


<style lang="less" scoped>
     #UpdateUserName{
     	position:fixed;
     	top:0;
     	left:0;
     	bottom:0;
     	right:0;
     	background-color:#f5f5f5;
     	z-index:200;
      overflow-y: scroll;

     }


    .router-slider-enter-active,.router-slider-leave-active{
      transition:all 0.3s;
    }

    .router-slider-enter,.router-slider-leave-active{
      transform:translate3d(2rem,0,0);
      opacity:0;
    }
</style>